<?php

class Application_Model_Mapper_CharterfirmaMapper{
	protected $_dbTable;

	public function setDbTable ($dbTable){
		if (is_string($dbTable)){
			$dbTable = new $dbTable();
		}
		if (! $dbTable instanceof Zend_Db_Table_Abstract){
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable (){
		if (null === $this->_dbTable){
			$this->setDbTable('Application_Model_DbTable_charterfirma');
		}
		return $this->_dbTable;
	}

	public function add (Application_Model_Charterfirma $object){
		$now = new DateTime();
		/**
		 * Fill data array
		 */
		
		$generatedid = Helper_Idgenerator::getid();
		$generatedidcheck = $this->fetchID($generatedid);
		if(empty($generatedidcheck)){
			$data = array(
					'id' =>$generatedid,
					'created' => date('Y-m-d H:i:s'),
					'lastupdater'=> $object->getLastupdater(), //id von ersteller mitschicken
					'lastupdated'=> date('Y-m-d H:i:s'),
					'name' => $object->getName(),
					'revier' => $object->getRevier(),
					'land' => $object -> getLand(),
					'stadt' => $object->getStadt(),
					'adresse' => $object->getAdresse(),
					'lat' =>$object->getLat(),
					'lng' =>$object->getLng(),
					'saisonzeitbeginn' => $object->getSaisonzeitbeginn(),
					'saisonzeitende' => $object->getSaisonzeitende(),
					'hafenweg' => $object->getHafenweg(),
					'open' => $object->getOpen(),
					'close' => $object->getClose(),
					'weblink' => $object->getWeblink(),
					'premium' => $object->getPremium(),
					'status' => $object->getStatus()
// 	 				'profilbild' => $object->getProfilbild()
			);
			return $this->getDbTable()->insert($data);
		}
		else{
			$this->add($object);
		}
	}

	
	public function search ($name, $revier, $land, $stadt, $adresse, $lat, $lng, $saisonzeitbeginn, $saisonzeitende, $hafenweg, $open, $close, $weblink, $premium, $formData){
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('charterfirma');

		if($name){
			$entry->where('charterfirma.name like "%' . $formData['name'] . '%"');
		}
		if($revier){
			$entry->where('charterfirma.revier = ?', $formData['revier']);
		}
		if($land){
			$entry->where('charterfirma.land = ?', $formData['land']);
		}
		if($stadt){
			$entry->where('charterfirma.stadt = ?', $formData['stadt']);
		}
		if($adresse){
			$entry->where('charterfirma.adresse like "%' . $formData['adresse'] . '%');
		}
		if($lat){
			$entry->where('charterfirma.lat = ?', $formData['lat']);
		}
		if($lng){
			$entry->where('charterfirma.lng = ?', $formData['lng']);
		}
		if($saisonzeitbeginn){
			$entry->where('charterfirma.saisonzeitbeginn = ?', $formData['saisonzeitbeginn']);
		}
		if($saisonzeitende){
			$entry->where('charterfirma.saisonzeitende = ?', $formData['saisonzeitende']);
		}
		if($hafenweg){
			$entry->where('charterfirma.hafenweg = ?', $formData['hafenweg']);
		}
		if($open){
			$entry->where('charterfirma.open = ?', $formData['open']);
		}
		if($close){
			$entry->where('charterfirma.close = ?', $formData['close']);
		}
		if($weblink){
			$entry->where('charterfirma.weblink = ?', $formData['weblink']);
		}
		return $entry->query()->fetchall();
	}
	
	public function update (Application_Model_Charterfirma $object){
		$data = array(
					'lastupdater'=> $object->getLastupdater(), //id von ersteller mitschicken
					'lastupdated'=> date('Y-m-d H:i:s'),
					'name' => $object->getName(),
					'revier' => $object->getRevier(),
					'land' => $object -> getLand(),
					'stadt' => $object->getStadt(),
					'adresse' => $object->getAdresse(),
					'lat' =>$object->getLat(),
					'lng' =>$object->getLng(),
					'saisonzeitbeginn' => $object->getSaisonzeitbeginn(),
					'saisonzeitende' => $object->getSaisonzeitende(),
					'hafenweg' => $object->getHafenweg(),
					'open' => $object->getOpen(),
					'close' => $object->getClose(),
					'weblink' => $object->getWeblink(),
// 	 				'profilbild' => $object->getProfilbild()
		);
		$table = $this->getDbTable();
		$where = $table->getAdapter()->quoteInto('id = ?' , $object->getId());
		$table->update($data, $where);
	}

	public function delete ($id){
		$table = $this->getDbTable();
		$where = $table->getAdapter()->quoteInto('id = ?', $id);
		$table->delete($where);
	}

	public function fetchID ($id){
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('charterfirma');
		$entry->where('charterfirma.id = ?', $id);
		return $entry->query()->fetch();
	}

	public function fetchAll(){
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('charterfirma');
		return $entry->query()->fetchall();
	}
}